/*
 * ObjectClassifier.c
 * VB Script Analyzer
 * @author Gregoire JACOB (gregoire.jacob@orange-ftgroup.com)
 * @date 07/04/2008
 * @version 1.0
 */

#include ".\Includes\Analyzer.h"

int ClassifyObject(char * name, int * nature){
	int t, max = 0, maxnat = *nature;
	//autoreference (must be an object not a variable)
	if(name&&*nature!=VAR&&
		(strcasestr(name,AUTO_REF)||strcasestr(name,AUTO_REFS)||
		 strcasestr(name,scriptname)||strcasestr(name,HTM_DOC))) return TYPE_THIS;
	switch(*nature){
		case OBJ_FILE:
		case OBJ_FOLD:
		case OBJ_DRIVE:
			if(name) return ClassifyFile(name,nature);
		case OBJ_REG:
			if(name) return ClassifyRegKey(name,nature);
			return TYPE_PERM;
		case OBJ_MAIL:
			return TYPE_COM;
		case VAR:
			return TYPE_VAR;
		default:
			//Checks for most significant types
			if(name){
				maxnat = OBJ_FILE;
				if((t=ClassifyFile(name,&maxnat))>max){
					if(t>TYPE_PERM){max = t; *nature = maxnat;}
				}
				maxnat = OBJ_REG;
				if((t=ClassifyRegKey(name,&maxnat))>max){
					if(t>TYPE_PERM){max = t; *nature = maxnat;}
				}
			}//
	}//switch
	return max;
}

int ClassifyFile(char * name, int * nature){
	//Configuration files (OS, mIRC)
	if(strcasestr(name,RUN_FILE1)||
		strcasestr(name,RUN_FILE2)||
		  strcasestr(name,RUN_FILE3)||
		    strcasestr(name,RUN_FILER)||
		     strcasestr(name,RUN_FILEM)){
		return TYPE_BOOT;
	}
	if(strcasestr(name,RUN_FOLD)){
		*nature = OBJ_FOLD;
		return TYPE_BOOT;
	}
	if(strcasestr(name,COM_DRIVE)){
		*nature = OBJ_DRIVE;
		return TYPE_COM;
	}
	if(strcasestr(name,COM_FOLD1)||
		strcasestr(name,COM_FOLD2)||
		strcasestr(name,COM_FOLD3)||
		strcasestr(name,COM_FOLD4)||
		strcasestr(name,COM_FOLD5)||
		strcasestr(name,COM_FOLD6)){
		*nature = OBJ_FOLD;
		return TYPE_COM;
	}
	if(name[1]==':'&& ((name[0]>='I' && name[0]<='Z')
		              || (name[0]>='i' && name[0]<='z'))){
		*nature = OBJ_DRIVE;
		return TYPE_COM;
	}
	return TYPE_PERM;
}

int ClassifyRegKey(char * name, int * nature){
	//run or start page registry key
	if(strcasestr(name,RUN_REG1)||
		strcasestr(name,RUN_REG2)||
			strcasestr(name,RUN_REG3)){
		return TYPE_BOOT;
	}
	return TYPE_PERM;
}